package internationalenergytradingplatform.controller.SupplierController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.ClassificationMapper;
import internationalenergytradingplatform.dao.CommodityMapper;
import internationalenergytradingplatform.dto.ClassificationPageQueryDTO;
import internationalenergytradingplatform.pojo.Classification;
import internationalenergytradingplatform.pojo.Commodity;
import internationalenergytradingplatform.pojo.Package;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classification")
@CrossOrigin
@Slf4j
public class ClassificationController {

    @Autowired
    ClassificationMapper classificationMapper;

    @Autowired
    CommodityMapper commodityMapper;

    @Autowired
    CommodityController commodityController;
    /**
     * 分页查看指定供应商的全部分类
     */
    @GetMapping
    public Result<PageResult> pagingToQueryAllClassification(ClassificationPageQueryDTO classificationPageQueryDTO) {
        String companyId = BaseContext.getCurrentId();//获取公司id
        classificationPageQueryDTO.setAccountr(companyId);
        PageHelper.startPage(classificationPageQueryDTO.getPage(), classificationPageQueryDTO.getPageSize());
        Page<Classification> page = classificationMapper.ClassificationPageQuery(classificationPageQueryDTO);
        long total = page.getTotal();
        List<Classification> records = page.getResult();
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }
    /**
     * 新增类别
     */
    @PostMapping("/classificationAdd")
    public Result<?> newClassficationAdd(@RequestBody Classification classification){
        String companyId = BaseContext.getCurrentId();//获取公司id
        classification.setAccountr(companyId);
        classificationMapper.addClassfication(classification);
        return Result.success();
    }

    /**
     * 修改类别
     */
    @PutMapping("/modify")
    public Result<?> modifyClassification(@RequestBody Classification classification){
        classificationMapper.modifyClassification(classification);
        return Result.success();
    }

    /**
     * 根据id查找类别
     */
    @GetMapping("/{id}")
    public Result<Classification> findClassificationById(@PathVariable Integer id){
        Classification classification = classificationMapper.getClassificationById(id);
        return Result.success(classification);
    }

    /**
     * 删除类别
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteClassificationById(@PathVariable Integer id){
        List<Commodity> allCommodityByClassification = commodityMapper.getAllCommodityByClassification(id);
        for(Commodity commodity : allCommodityByClassification) {
            commodity.setClassification(5);
            commodityController.modifyCommodity(commodity);
        }
        classificationMapper.deleteClassificationById(id);
        return Result.success();
    }


}
